const _conf = require('config');
const _toast = require('toast');
// const dieLog = require('dieLog');

let ajaxDeug = false;
console.log(ajaxDeug);
const request = options => {
  const successFun = options.success;
  delete options.success;
  const failFun = options.fail;
  delete options.fail;
  const completeFun = options.complete;
  delete options.complete;
  if (options.is_loading && options.is_loading.status && !options.is_loading.hideShow) {
    // if (options.is_loading.title) {
    wx.showLoading({
      title: options.is_loading.title,
      mask:true,
      complete: () => {
        console.log(options.is_loading.title)
      }
    })
    // } else {
    //   _toast.showLoading();
    // }

  }
  const app = getApp();
  let loginInfo = false;
  try {
    loginInfo = wx.getStorageSync('loginInfo') ? wx.getStorageSync('loginInfo') : app.globalData.loginInfo;
  } catch (e) {
    console.log(e)
  }
  // ajaxDeug = loginInfo.wx_mini_debug || false;
  // console.log(ajaxDeug);
  let callBackNoLoading;
  let defaultConf = {
    data: {},
    method: "POST",
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    success: res => {
      if (res.data.code == 0 ) {
        callBackNoLoading = () => {
          typeof successFun == 'function' && successFun(res.data.data);
        }
      } else {
        switch (parseInt(res.data.code)) {
          case 400:
          case 510:
            // app.globalData.jsCode = null;
          case 511:
            callBackNoLoading = () => {
              _toast.showToast({
                title: res.data.msg,
                type: 'error',
              })
            }
            break;
          case 401:
            callBackNoLoading = () => {
              app.redirectWithData(_conf.loginPage, function () {
                _toast.showToast({
                  title: res.data.msg,
                  type: 'error',
                })
              });
            }
            break;
          default:
            callBackNoLoading = () => {
              typeof options.error == 'function' && options.error(res.data);
            }
            break;
        }
      }


    },
    fail: (res) => {
      wx.showToast({
        title: '网络错误',
        icon: 'error',
        duration: _conf.toastConfig.duration,
        mask: true
      })
      callBackNoLoading = () => {
        typeof failFun == 'function' && failFun(res.data);
      }
    },
    complete: res => {
      if (options.is_loading && options.is_loading.status) {
        console.log('loading关闭')
        wx.hideLoading({
          complete: () => {}
        })
        typeof callBackNoLoading == 'function' && callBackNoLoading(res.data);
      } else {
        typeof callBackNoLoading == 'function' && callBackNoLoading(res.data);
      };
      if (ajaxDeug) {
        console.error(res);
      }
      typeof completeFun == 'function' && completeFun(res.data)

    }
  }
  const requestConf = Object.assign(defaultConf, options);

  requestConf.url = requestConf.pre_url ? requestConf.pre_url + requestConf.url : _conf.baseApi + requestConf.url;

  requestConf.header = {
    'content-type': (options.header == undefined || options.header['content-type'] == undefined) ?
      'application/x-www-form-urlencoded' : options.header['content-type'], // 默认值
    'Authorization': !loginInfo ? '' : "Bearer " + loginInfo.access_token,
    'token': !loginInfo ? '' : loginInfo.token,
    'openid': !loginInfo ? '' : loginInfo.openid,
    'version': _conf.version
  };
  if (ajaxDeug) {
    console.error(requestConf);
  }
  app.globalData.last_request_task = wx.request(requestConf);
}

module.exports = request;